package com.demo.FileOutputStream_;

import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.*;

/**
 * @author gy
 * @date 2022/4/5
 */
public class BufferedByteStreamCopy02 {
    private static final Logger logger = Logger.getLogger(BufferedByteStreamCopy02.class);

    @Test
    public void m1() {
        // 图片文件路径
//        String srcFilePath = "f:\\20220403\\壁纸.png";
//        String targetFilePath = "f:\\20220403\\壁纸2.png";
        // 音乐文件路径
        String srcFilePath = "f:\\20220403\\谭艳 - 光明 (DJ奶小深版).mp3";
        String targetFilePath = "f:\\20220403\\谭艳 - 光明 (DJ奶小深版)03.mp3";
        // 文本文件路径
//        String srcFilePath = "f:\\20220403\\bufferedCopy01.txt";
//        String targetFilePath = "f:\\20220403\\bufferedCopy02.txt";
        
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        byte[] buff = new byte[1024];
        int readLength = 0;
        try {
            bis = new BufferedInputStream(new FileInputStream(srcFilePath));
            bos = new BufferedOutputStream(new FileOutputStream(targetFilePath));

            while ((readLength = bis.read(buff)) != -1){
                // 边读边写
                bos.write(buff,0,readLength);
            }
            logger.info("文件"+srcFilePath+"拷贝成功");

        } catch (IOException e) {
            logger.error(e.getMessage());
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
